#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
交易 - 退款功能使用示例
"""

import os
import sys

# 添加项目根目录到Python路径，确保能够导入jlpay包
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))

from examples.base_example import BaseExample
from jlpay.services.trans.common.refund_service import RefundService
from jlpay.model.trans.common.common_trans import RefundRequest


class RefundExample(BaseExample):
    """退款示例类"""
    
    def __init__(self):
        """初始化示例类"""
        super().__init__("examples.trans.common.refund")
        self.service = RefundService(self.config)
    
    def build_refund_request(self):
        """
        构建退款请求
        
        Returns:
            RefundRequest实例
        """
        request = RefundRequest()
        
        # 设置请求参数
        request.set_mch_id("849584358120018")
        request.set_out_trade_no("ld201911023465529")
        request.set_ori_out_trade_no("ld2019102216465525")
        request.set_ori_transaction_id("80011557201909171853011568")
        request.set_total_fee("1")
        request.set_refund_fee("50")
        request.set_remark("测试退款")
        request.set_mch_create_ip("127.0.0.1")
        request.set_longitude("116.397128")
        request.set_latitude("39.916527")
        
        return request
    
    def demo(self):
        """执行退款示例"""
        self.logger.info("开始执行退款示例")
        
        # 创建退款请求
        request = self.build_refund_request()
        
        # 发送请求
        self.send_request_and_handle_response(
            self.service.refund,
            "退款",
            request
        )
        
        self.logger.info("退款示例执行完毕")


if __name__ == "__main__":
    logger = RefundExample().logger
    logger.info("=" * 50)
    logger.info("===== 退款示例 开始 =====")
    logger.info("=" * 50)
    
    example = RefundExample()
    example.demo()
    
    logger.info("=" * 50)
    logger.info("===== 退款示例执行完毕 =====")
    logger.info("=" * 50) 